<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-notification"></i> 公告管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box" style="margin-bottom:50px">
                <el-col :span="6">
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                    >批量删除</el-button>
                    <el-button
                        type="primary"
                        icon="el-icon-circle-plus-outline"
                        class="handle-del mr10"
                        @click="handleAdd"
                    >添加公告</el-button>
                </el-col>
                <el-col :span="12">
                    <el-select v-model="query.sort" placeholder="排序" class="handle-select mr10">
                        <el-option key="1" label="正序" value="1"></el-option>
                        <el-option key="2" label="倒序" value="2"></el-option>
                    </el-select>
                    <el-select v-model="query.status" placeholder="状态" class="handle-select mr10">
                        <el-option label="全部" value></el-option>
                        <el-option key="1" label="正常" value="1"></el-option>
                        <el-option key="2" label="禁用" value="2"></el-option>
                    </el-select>
                    <el-input v-model="query.name" placeholder="公告名称" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </el-col>
            </div>
            <el-table
                :data="tableData"
                border
                v-loading="loading"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="70" sortable align="center"></el-table-column>
                <el-table-column prop="name" label="名称"></el-table-column>
                <el-table-column label="排序" prop="sort" sortable="custom"></el-table-column>
                <el-table-column label="图片(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.img_url"
                            :preview-src-list="[scope.row.img_url]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态" sortable align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.status==1?'success':'danger'"
                        >{{scope.row.status==1?'正常':'禁用'}}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="updated_at" sortable label="修改时间"></el-table-column>
                <el-table-column prop="created_at" sortable label="添加时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog title="添加公告" v-dialogDrag :visible.sync="addVisible" width="50%">
            <el-form ref="edit" :model="add" label-width="70px">
                <el-form-item label="名称" class="form-item">
                    <el-input v-model="add.name" maxlength="30" minlength="2" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="add.status" active-value="1" inactive-value="2"></el-switch>
                </el-form-item>
                <el-form-item label="排序" class="form-item">
                    <el-input
                        v-model="add.sort"
                        maxlength="30"
                        type="number"
                        minlength="2"
                        placeholder="请输入排序"
                    ></el-input>
                </el-form-item>
                <el-form-item label="上传图片">
                    <el-upload
                        class="upload-demo"
                        ref="addUploads"
                        drag
                        :limit="1"
                        :auto-upload="true"
                        show-file-list
                        accept="image/*"
                        :on-success="uploadSuccess"
                        :on-error="uploadError"
                        :on-preview="uploadPreview"
                        :action="api.upload_img_url"
                        list-type="picture"
                        :on-exceed="uploadExceed"
                        :file-list="add.img_url"
                        multiple
                    >
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">
                            将文件拖到此处，或
                            <em>点击上传</em>
                        </div>
                        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="详情" label-width="80px">
                    <editor-bar v-model="add.desc" :isClear="true" @change="contentChange2"></editor-bar>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑公告" v-dialogDrag :visible.sync="editVisible" width="50%">
            <el-form ref="edit" :model="edit" label-width="70px">
                <el-form-item label="名称" class="form-item">
                    <el-input v-model="edit.name" maxlength="30" minlength="2" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="edit.status" active-value="1" inactive-value="2"></el-switch>
                </el-form-item>
                <el-form-item label="排序" class="form-item">
                    <el-input
                        v-model="edit.sort"
                        maxlength="30"
                        type="number"
                        minlength="2"
                        placeholder="请输入排序"
                    ></el-input>
                </el-form-item>
                <el-form-item label="上传图片">
                    <el-upload
                        class="upload-demo"
                        ref="editUploads"
                        drag
                        :limit="1"
                        :auto-upload="true"
                        show-file-list
                        accept="image/*"
                        :on-success="editUploadSuccess"
                        :on-error="editUploadError"
                        :on-preview="editUploadPreview"
                        list-type="picture"
                        :action="api.upload_img_url"
                        :file-list="[{name:'预览图',url:edit.img_url}]"
                        :on-exceed="uploadExceed"
                        multiple
                    >
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">
                            将文件拖到此处，或
                            <em>点击上传</em>
                        </div>
                        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="详情" label-width="80px">
                    <editor-bar v-model="edit.desc" :isClear="true" @change="contentChange2"></editor-bar>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 预览图片 -->
        <div class="previews" @click="closePreviews" v-if="windowsPopVisible">
            <img :src="windowsPopUrl" title="点击即可关闭预览" />
        </div>
    </div>
</template>

<script>
    import EditorBar from '../common/wangEnduit';

    export default {
    components: { EditorBar },
   name: 'notice',
  data () {
    return {
      query: {
        status: '',
        sort: '',
        name: '',
        page: 1,
        pageSize: 10
      },
      loading: false,
      tableData: [],
      windowsPopVisible: false,
      windowsPopUrl: '',
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      edit: {},
      add: {
        img: '',
        name: '',
        status: '1',
        sort: 10,
        url: '',
        img_url: [],
        desc:''
      },
      idx: -1,
      id: -1,
      getList: function () {
        let that = this;
        this.api.get('getNotice', this.query, res => {
          let data = res.data;
          for (let i in data.list) {
            data.list[i].status = data.list[i].status + '';
          }
          that.tableData = data.list;
          that.pageTotal = data.pageTotal;
        }, undefined, undefined, this);
      },
      delNotice: function (ids, msg) {
        let that = this;
        if (!ids || ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delNotice(ids, function (response) {
            that.multipleSelection = [];
            that.query.page = 1;
            that.getList();
            that.$message.success(msg);
          });
        }).catch(() => { });
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
      contentChange2 (val) {
          this.add.desc = val;
      },
    /***
     * 超出上传限制事件
     */
    uploadExceed () {
      this.$message.error('只能上传一张图片');
    },

    /***
     * 添加公告预览
     */
    uploadPreview (file) {
      this.windowsPopVisible = true;
      this.windowsPopUrl = file.response.data.url;
    },

    /***
     * 关闭预览
     */
    closePreviews () {
      this.windowsPopVisible = false;
    },

    /***
     * 添加公告上传成功事件
     */
    uploadSuccess (response, file, fileList) {
      if (response.code != 1000) {
        this.$message.error(response.msg);
        return false;
      }
      this.add.img = response.data.path;
      this.add.img_url = [{ name: '预览', url: response.data.url }];
    },
    /***
     * 添加公告上传失败事件
     */
    uploadError (response, file, fileList) {
      this.$message.error('上传失败');
    },

    /****
     * 编辑公告预览
     */
    editUploadPreview (info) {
      this.windowsPopUrl = info.url;
      this.windowsPopVisible = true;
    },

    /***
     * 编辑上传成功事件
     */
    editUploadSuccess (response) {
      if (response.code != 1000) {
        this.$message.error(response.msg);
        return false;
      }
      this.edit.img = response.data.path;
      this.edit.img_url = response.data.url;
    },

    /***
     * 编辑上传失败事件
     */
    editUploadError () {
      this.$message.error('上传失败');
    },

    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },
    // 删除操作
    handleDelete (index, row) {
      this.delNotice([row.id], '删除成功');
    },
    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delNotice(ids, `删除了${str}`);
    },
    // 编辑操作
    handleEdit (index, row) {

      this.idx = index;
      this.edit = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit () {
      let that = this;
      that.api.editNotice(that.edit, function () {
        that.$message.success('保存成功');
        that.editVisible = false;
        that.getList();
      });
    },
    /***
     * 添加操作
     */
    handleAdd () {
      this.add = {
        img: '',
        name: '',
        status: '1',
        sort: 10,
        img_url: []
      };
      this.windowsPopUrl = '';
      this.addVisible = true;
    },
    /***
     * 保存添加
     */
    saveAdd () {
      let that = this;
      that.api.addNotice(that.add, function () {
        that.$message.success('添加成功');
        that.addVisible = false;
        that.getList();
      });
    },
    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    }

  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}
.form-item {
    width: 70%;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.previews {
    display: flex;
    flex-flow: nowrap row;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: fixed;
    z-index: 9999999999;
    background: rgba(0, 0, 0, 0.3);
    justify-content: center;
    align-items: center;
}
.previews > img {
    max-width: 90%;
    max-height: 90%;
}
.el-image-viewer__close {
    color: #ffffff;
}
</style>
